var toupiao_type;
var link_url;

// start 创建 XmlHTTPRequest 对象
function createXmlHttpRequestObject() {
   // will store the reference to the XMLHttpRequest object
   var xmlHttp;
   // this should work for all browsers except IE6 and older
   try
   {
     // try to create XMLHttpRequest object
     xmlHttp = new XMLHttpRequest();
   }
   catch(e)
   {
     // assume IE6 or older
     var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                     "MSXML2.XMLHTTP.5.0",
                                     "MSXML2.XMLHTTP.4.0",
                                     "MSXML2.XMLHTTP.3.0",
                                     "MSXML2.XMLHTTP",
                                     "Microsoft.XMLHTTP");
     // try every prog id until one works
     for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
     {
       try
       {
         // try to create XMLHttpRequest object
         xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
       }
       catch (e) {}
     }
   }
   // return the created object or display an error message
   if (!xmlHttp)
     alert("Error creating the XMLHttpRequest object.");
   else
     return xmlHttp;
}

var xmlHttp = createXmlHttpRequestObject();
// end 创建 XmlHttpRequest 对象

// start 删除链接
function del_link(url) {
	if(!confirm('您确定要删除此链接吗？')) {
		return false;
	}
	link_url = url;
	var url = "del_link.php?url="+link_url;
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange = callback_link;
	xmlHttp.send(null);
}
// end 删除链接

// start 删除链接的回调函数
function callback_link() {
	if(xmlHttp.readyState==4 && xmlHttp.status==200) {
		var tr = document.getElementById(link_url);
		tr_2_id = link_url+"_2";
		var tr_2 = document.getElementById(tr_2_id);
		tr.style.display = "None";
		tr_2.style.display = "None";
		var message_box = document.getElementById("message_box")
		message_box.style.display = "block";
		message_box.innerHTML = xmlHttp.responseText;
	}
}
// end 删除链接的回调函数

function add_zhichi_fandui(addr,type,article_id) {
	document.getElementById("isnot_toupiao").innerHTML = "正在建立链接 |"
	toupiao_type = type;
	var url = "add_zhichi_fandui.php?ip="+addr+"&type="+toupiao_type+"&article_id="+article_id;
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange = callback;
	xmlHttp.send(null);
}

// 投票/支持/反对的回调函数
function callback() {
	if(xmlHttp.readyState==4 && xmlHttp.status==200) {
		var zhichi_span = document.getElementById("zhichi");
		var fandui_span = document.getElementById("fandui");
		var isnot_toupiao = document.getElementById("isnot_toupiao");
		var count = xmlHttp.responseText;
		if (count == -1) {
			isnot_toupiao.innerHTML = "您已经投过票了！ |";
		} else {
			if (toupiao_type=="zhichi") {
				zhichi_span.innerHTML = count;
			} else if (toupiao_type=="fandui") {
				fandui_span.innerHTML = count;
			}
			isnot_toupiao.innerHTML = "谢谢您的参与！ ｜"
		}
	}
}
// 投票/支持/反对的回调函数 END

// 置顶
function goTop(acceleration, time) {
	acceleration = acceleration || 0.1;
	time = time || 16;

	var dx = 0;
	var dy = 0;
	var bx = 0;
	var by = 0;
	var wx = 0;
	var wy = 0;

	if (document.documentElement) {
		dx = document.documentElement.scrollLeft || 0;
		dy = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		bx = document.body.scrollLeft || 0;
		by = document.body.scrollTop || 0;
	}
	var wx = window.scrollX || 0;
	var wy = window.scrollY || 0;

	var x = Math.max(wx, Math.max(bx, dx));
	var y = Math.max(wy, Math.max(by, dy));

	var speed = 1 + acceleration;
	window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
	if(x > 0 || y > 0) {
		var invokeFunction = "goTop(" + acceleration + ", " + time + ")"
		window.setTimeout(invokeFunction, time);
	}
}
// 置顶 END
